package com.yingpai.fitness.dvp_2_0.main.widget.calendar;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.WeekView;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.util.DateFormatUtil;

/**
 * Created by ASUS on 2018/11/20.
 */

public class SignCalendarWeekView extends WeekView {
    //    private Paint paint = new Paint();
    private int mRadius;
    private Bitmap mBitmap;

    @Override
    public void onClick(View v) {
//        super.onClick(v);
    }

    public SignCalendarWeekView(Context context) {
        super(context);
//        paint.setAntiAlias(true);
//        paint.setStyle(Paint.Style.STROKE);
//        paint.setStrokeWidth(dipToPx(context, 1f));
//        paint.setColor(getResources().getColor(R.color.color_red));

        mBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.counter_hook);
    }

    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 11 * 4;
    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, boolean hasScheme) {
        int cx = x + mItemWidth / 2;
        int cy = mItemHeight / 2;

        if(calendar.toString().equals(DateFormatUtil.getTodayshortdate())){
            canvas.drawCircle(cx, cy, mRadius/4*5, mSelectedPaint);
        }

        if (hasScheme) {
            canvas.drawBitmap(mBitmap, cx - mRadius + 15, cy + mRadius - 20, null);
//            canvas.drawLine(cx - mRadius + 20, cy + mRadius - 10, cx, cy + mRadius + 5, paint);
//            canvas.drawLine(cx, cy + mRadius + 5, cx + mRadius - 15, cy + mRadius - 15, paint);
        }
        return false;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x) {
        int cx = x + mItemWidth / 2;
        int cy = mItemHeight / 2;
        canvas.drawBitmap(mBitmap, cx - mRadius + 15, cy + mRadius - 20, null);
//        canvas.drawLine(cx - mRadius + 20, cy + mRadius - 10, cx, cy + mRadius + 5, paint);
//        canvas.drawLine(cx, cy + mRadius + 5, cx + mRadius - 15, cy + mRadius - 15, paint);
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine;
        int cx = x + mItemWidth / 2;
        if(calendar.toString().equals(DateFormatUtil.getTodayshortdate())){
            mSelectTextPaint.setColor(getResources().getColor(R.color.white));
        }else{
            mSelectTextPaint.setColor(getResources().getColor(R.color.calendar_textColor));
        }
        if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    mSelectTextPaint);
        } else if (hasScheme) {
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);

        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }


    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    @SuppressWarnings("all")
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
